import { api } from './api'
import type { ApiResponse, PaginatedResponse, PaginationParams } from '../types'

interface SystemSetting {
  id: number
  key: string
  value: string
  description?: string
  type: 'STRING' | 'NUMBER' | 'BOOLEAN' | 'JSON'
  group: string
  public: boolean
  createdAt: string
  updatedAt: string
}

interface CreateSettingRequest {
  key: string
  value: string
  description?: string
  type: SystemSetting['type']
  group: string
  public?: boolean
}

interface UpdateSettingRequest {
  value: string
  description?: string
  public?: boolean
}

interface GetSettingsParams extends PaginationParams {
  group?: string
  public?: boolean
}

export const settingsApi = api.injectEndpoints({
  endpoints: (builder) => ({
    getSettings: builder.query<ApiResponse<PaginatedResponse<SystemSetting>>, GetSettingsParams>({
      query: (params) => ({
        url: '/settings',
        params,
      }),
      providesTags: (result) =>
        result
          ? [
              ...result.data.content.map(({ id }) => ({ type: 'Setting' as const, id })),
              { type: 'Setting' as const, id: 'LIST' },
            ]
          : [{ type: 'Setting' as const, id: 'LIST' }],
    }),

    getAllSettings: builder.query<ApiResponse<SystemSetting[]>, void>({
      query: () => '/settings/all',
      providesTags: [{ type: 'Setting', id: 'LIST' }],
    }),

    getSetting: builder.query<ApiResponse<SystemSetting>, string>({
      query: (key) => `/settings/${key}`,
      providesTags: (result) =>
        result ? [{ type: 'Setting' as const, id: result.data.id }] : [],
    }),

    createSetting: builder.mutation<ApiResponse<SystemSetting>, CreateSettingRequest>({
      query: (data) => ({
        url: '/settings',
        method: 'POST',
        body: data,
      }),
      invalidatesTags: [{ type: 'Setting', id: 'LIST' }],
    }),

    updateSetting: builder.mutation<ApiResponse<SystemSetting>, { key: string; data: UpdateSettingRequest }>({
      query: ({ key, data }) => ({
        url: `/settings/${key}`,
        method: 'PUT',
        body: data,
      }),
      invalidatesTags: (result) =>
        result ? [{ type: 'Setting', id: result.data.id }] : [],
    }),

    deleteSetting: builder.mutation<ApiResponse<void>, string>({
      query: (key) => ({
        url: `/settings/${key}`,
        method: 'DELETE',
      }),
      invalidatesTags: [{ type: 'Setting', id: 'LIST' }],
    }),

    getPublicSettings: builder.query<ApiResponse<Record<string, string>>, void>({
      query: () => '/settings/public',
      providesTags: ['Setting'],
    }),

    importSettings: builder.mutation<ApiResponse<void>, FormData>({
      query: (data) => ({
        url: '/settings/import',
        method: 'POST',
        body: data,
      }),
      invalidatesTags: [{ type: 'Setting', id: 'LIST' }],
    }),

    exportSettings: builder.query<Blob, void>({
      query: () => ({
        url: '/settings/export',
        responseHandler: (response) => response.blob(),
      }),
    }),
  }),
})

export const {
  useGetSettingsQuery,
  useGetAllSettingsQuery,
  useGetSettingQuery,
  useCreateSettingMutation,
  useUpdateSettingMutation,
  useDeleteSettingMutation,
  useGetPublicSettingsQuery,
  useImportSettingsMutation,
  useExportSettingsQuery,
} = settingsApi 